'''组织架构页面'''
from time import sleep
from selenium.common import NoSuchElementException
from selenium.webdriver import Keys
from framwork.pages.base_page import BasePage
# 引入by方法
from selenium.webdriver.common.by import By

class OrganizationPage(BasePage):
    """先写定位器，再写方法"""
    add_department_locator = (By.ID, "add_department")  # 添加部门
    bm_btn_locator = (By.CSS_SELECTOR, "#name")  # 部门名
    department_success_locator = (By.CSS_SELECTOR, "body > div.container > div.alert.alert-success")
    department_error_locator = (By.CSS_SELECTOR, "body > div.container > div.alert.alert-error")

    add_role_locator = (By.ID, "add_role")  # 添加岗位
    gw_name_locator = (By.CSS_SELECTOR, "#role_add > div:nth-child(1) > div > input")  # 岗位名

    add_role_error_locator = (By.CSS_SELECTOR, "body > div.container > div.alert.alert-error")
    add_role_success_locator = (By.CSS_SELECTOR, "body > div.container > div.alert.alert-success")

    # excepted_text = "添加岗位成功！"
    # error_excepted_text = "请填写岗位名称"

    add_user_locator = (By.CSS_SELECTOR, "#user_form > div:nth-child(1) > div > a:nth-child(3)")

    def click_add_user(self):
        '''点击添加用户'''
        self.find_element(self.add_user_locator).click()

    def click_add_department(self):
        '''点击添加部门'''
        self.find_element(self.add_department_locator).click()

    def input_department_name(self, department_name):
        '''输入部门名称'''
        self.find_element(self.bm_btn_locator).send_keys(department_name)

    def click_save_department(self):
        '''点击保存部门'''
        self.find_element(self.bm_btn_locator).send_keys(Keys.ENTER)

    def add_department_seccess_text(self):
        '''添加部门成功提示文本，用于断言'''
        try:
            text = self.find_element(self.department_success_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def add_department_error_text(self):
        '''添加部门失败提示文本，用于断言'''
        try:
            text = self.find_element(self.department_error_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def click_add_role(self):
        '''点击添加岗位'''
        self.find_element(self.add_role_locator).click()

    def input_role_name(self, role_name):
        '''输入岗位名'''
        self.find_element(self.gw_name_locator).send_keys(role_name)

    def click_save_role(self):
        '''点击保存岗位'''
        self.find_element(self.gw_name_locator).send_keys(Keys.ENTER)

    def add_role_success_text(self):
        '''添加岗位成功提示文本，用于断言'''
        try:
            text = self.find_element(self.add_role_success_locator).text
        except NoSuchElementException:
            text = ""
        return text

    def add_role_error_text(self):
        '''添加岗位失败提示文本，用于断言'''
        try:
            text = self.find_element(self.add_role_error_locator).text
        except NoSuchElementException:
            text = ""
        return text



